home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / DataFlavor.java < prev    next >
Text File  |  1998-09-22  |  8KB  |  226 lines

  1. /*
  2.  * @(#)DataFlavor.java    1.6 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.datatransfer;
  16.  
  17. import java.io.InputStream;
  18.  
  19. /**
  20.  * Each instance represents the opaque concept of a data format as would
  21.  * appear on a clipboard, during drag and drop, or in a file system.
  22.  *
  23.  * @version     1.6, 07/01/98
  24.  * @author    Blake Sullivan
  25.  */
  26. public class DataFlavor {
  27.  
  28.     static Class ioInputStreamClass = java.io.InputStream.class;
  29.  
  30.     /*
  31.      * private initializer
  32.      */
  33.  
  34.     static private DataFlavor createConstant(Class rc, String prn) {
  35.     try {
  36.         return new DataFlavor(rc, prn);
  37.     } catch (Exception e) {
  38.         return null;
  39.     }
  40.     }
  41.  
  42.     /*
  43.      * private initializer
  44.      */
  45.  
  46.     static private DataFlavor createConstant(String mt, String prn) {
  47.     try {
  48.         return new DataFlavor(mt, prn);
  49.     } catch (Exception e) {
  50.         return null;
  51.     }
  52.     }
  53.  
  54.    /**
  55.     * The DataFlavor representing a Java Unicode String class, where:
  56.     * <p>
  57.     * representationClass = java.lang.String<br>
  58.    /**
  59.     * The DataFlavor representing a Java Unicode String class, where:
  60.     * <p>
  61.     * representationClass = java.lang.String<br>
  62.     * mimeType            = "application/x-java-serialized-object"        
  63.     * <p> 
  64.     */
  65.     public final static DataFlavor stringFlavor = createConstant(java.lang.String.class, "Unicode String");
  66.  
  67.    /**
  68.     * The DataFlavor representing plain text with unicode encoding, where:
  69.     * <p>
  70.     * representationClass = InputStream<br>
  71.     * mimeType            = "text/plain; charset=unicode"        
  72.     * <p> 
  73.     */
  74.     public final static DataFlavor plainTextFlavor = createConstant("text/plain;charset=unicode", "Plain Text");
  75.         
  76.     static final String serializedObjectMimeType = "application/x-java-serialized-object";
  77.  
  78.     /* placeholder for caching any platform-specific data for flavor */
  79.     int atom;      
  80.   
  81.    /**
  82.     * Mime type for this DataFlavor.  (See RFC 1521 for an explanation
  83.     * of Mime types)  The type name is stored internally in the following
  84.     * cannonical order to make comparisons simpler
  85.     *   1. type, subtype, and parameter names are converted to lowercase
  86.     *
  87.     *   2. parameters are ordered by parameter name
  88.     *
  89.     *   3. character set parameter names are converted to lowercase (they
  90.     *      are the exception to the rule that parameter names should be
  91.     *      case sensitive
  92.     *
  93.     *   4. White space is compressed
  94.     *
  95.     */
  96.     private String mimeType;
  97.     
  98.     /** Human-presentable name for this DataFlavor. Localizable. **/
  99.     private String humanPresentableName;  
  100.   
  101.     /** Java class of objects this DataFlavor represents **/
  102.     private Class representationClass;
  103.   
  104.    /**
  105.     * Construct a DataFlavor that represents a Java class
  106.     * <p>
  107.     * The returned DataFlavor will have the following characteristics
  108.     * <p>
  109.     * representationClass = representationClass<br>
  110.     * mimeType            = application/x-java-serialized-object        
  111.     * <p>
  112.     * @param representationClass the class used to transfer data in this flavor
  113.     * @param humanPresentableName the human-readible string used to identify this flavor
  114.     */
  115.     public DataFlavor(Class representationClass, String humanPresentableName) {
  116.     this.mimeType = serializedObjectMimeType;
  117.     this.representationClass = representationClass;
  118.           this.humanPresentableName = humanPresentableName;
  119.     }
  120.  
  121.    /**
  122.     * Construct a DataFlavor that represents a MimeType
  123.     * <p>
  124.     * The returned DataFlavor will have the following characteristics:
  125.     * <p>
  126.     * If the mimeType is
  127.     * "application/x-java-serialized-object; class=<representation class>",
  128.     * the result is the same as calling
  129.     * new DataFlavor(Class:forName(<representation class>) as above
  130.     * <p>
  131.     * otherwise:
  132.     * <p>
  133.     * representationClass = InputStream<br>
  134.     * mimeType            = mimeType         
  135.     * <p>
  136.     * @param mimeType the string used to identify the MIME type for this flavor
  137.     * @param humanPresentableName the human-readible string used to identify this flavor
  138.     */
  139.     public DataFlavor(String mimeType, String humanPresentableName) {
  140.     this.mimeType = mimeType;
  141.     this.representationClass = ioInputStreamClass;
  142.           this.humanPresentableName = humanPresentableName;
  143.     }       
  144.   
  145.    /**
  146.     * Returns the MIME type string for this DataFlavor
  147.     */ 
  148.     public String getMimeType() {
  149.         return mimeType;
  150.     }
  151.  
  152.    /**
  153.     *  Returns the Class which objects supporting this DataFlavor
  154.     *  will return when this DataFlavor is requested.
  155.     */
  156.     public Class getRepresentationClass() {
  157.         return representationClass;
  158.     }
  159.   
  160.    /**
  161.     * Returns the human presentable name for the data foramt that this
  162.     * DataFlavor represents.  This name would be localized for different
  163.     * countries
  164.     */
  165.     public String getHumanPresentableName() {
  166.         return humanPresentableName;
  167.     }
  168.   
  169.    /**
  170.     * Sets the human presentable name for the data format that this
  171.     * DataFlavor represents. This name would be localized for different
  172.     * countries
  173.     */
  174.     public void setHumanPresentableName(String humanPresentableName) {
  175.         humanPresentableName = humanPresentableName;
  176.     }
  177.  
  178.     public boolean equals(DataFlavor dataFlavor) {
  179.     return (isMimeTypeEqual(dataFlavor) &&
  180.          dataFlavor.getRepresentationClass() == representationClass);
  181.     }
  182.   
  183.     /**
  184.      * Is the string representation of the MIME type passed in equivalent
  185.      * to the MIME type of this DataFlavor.  This may involve adding default
  186.      * attributes for some MIME types (like adding charset=US-ASCII to
  187.      * text/plain MIME types that have no charset parameter specified)
  188.      */
  189.     public boolean isMimeTypeEqual(String mimeType) {
  190.     // This is too simplistic
  191.     return mimeType.equals(this.mimeType);
  192.     }
  193.   
  194.     /**
  195.      * Convenience function equivalent to calling:
  196.      * isMimeTypeEqual(dataFlavor.getMimeType());
  197.      */
  198.     public final boolean isMimeTypeEqual(DataFlavor dataFlavor) {
  199.     return isMimeTypeEqual(dataFlavor.getMimeType());
  200.     }
  201.     
  202.    /**
  203.     * Called on DataFlavor for every MIME Type parameter to allow DataFlavor
  204.     * subclasses to handle special parameters like the text/plain charset
  205.     * parameters, whose values are case insensitive.  (MIME type parameter
  206.     * values are supposed to be case sensitive.
  207.     * <p>
  208.     * This method is called for each parameter name/value pair and should
  209.     * return the normalized representation of the parameterValue
  210.     */
  211.     protected String normalizeMimeTypeParameter(String parameterName, String parameterValue) {
  212.     return parameterName+"="+parameterValue;    
  213.     }
  214.   
  215.    /**
  216.     * Called for each MIME type string to give DataFlavor subtypes the
  217.     * opportunity to change how the normalization of MIME types is accomplished.
  218.     * One possible use would be to add default parameter/value pairs in cases
  219.     * where none are present in the MIME type string passed in
  220.     */
  221.     protected String normalizeMimeType(String mimeType) {
  222.     return mimeType;    
  223.     }
  224. }
  225.  
  226.